<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/6/13
 * Time: 23:51
 */
namespace App\Models\Api;
use App\Exceptions\ApiException;
use App\Models\Api\Base;
use Cache;

/**逻辑更新模型
 * Class SystemLogicVersion
 * @package app\common\mode
 */
class SystemLogicVersion extends Base
{
    protected $table = 'admin_logic_version';
    /**
     * 获取逻辑版本信息
     * @param array $map
     * rid int
     * type tinyint
     * @return bool|int|mixed
     */
    public function getVersion($map = [])
    {
        $arr = [
            'version'=>0,
            'title'=>'',
            'content'=>''
        ];

        if ($map['type'])
        {
            $data = false;
            if (intval($this->cache_on) >= 1 && Cache::has('logic_info_' . $map['type']))
            {
                $data = Cache::get('logic_info_' . $map['type']);
            }

            if ($data === false)
            {
                $where =['type'=>$map['type'],'status'=>1];
                try
                {
                    $data = $this->select('version','title','content')->where($where)->orderBy('version','DESC')->first()->toArray();

                    if (!$data)
                    {
                        $data = $arr;
                    }

                    if (intval($this->cache_on) >= 1)
                    {
                        Cache::set('logic_info_' . $map['type'], $data, 86400 * 7);
                    }

                }
                catch (\Exception $e)
                {
                    $data = $arr;
                }

            }
            return $data;
        }else{
            return $arr;
        }
    }


}